/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package com.eit.ntc.lmsbackend.validator;

import com.eit.ntc.lmsbackend.struts.form.LMSForm;
import java.util.Locale;
import java.util.ResourceBundle;
import org.apache.struts.upload.FormFile;
import org.springframework.context.support.DefaultMessageSourceResolvable;
import org.springframework.context.support.ResourceBundleMessageSource;
import org.springframework.validation.Errors;
import org.springframework.validation.ValidationUtils;
import org.springframework.validation.Validator;
import org.springframework.web.servlet.LocaleResolver;
import org.springframework.web.servlet.i18n.AbstractLocaleResolver;
import org.springframework.web.servlet.i18n.SessionLocaleResolver;
/**
 *
 * @author suporn
 */
public class LMSValidator implements Validator  {

    private static Locale locale = new Locale("th","TH");
    private ResourceBundleMessageSource resource;
    
    private static ResourceBundle resource1 = ResourceBundle.getBundle("com/eit/ntc/lmsbackend/struts/ApplicationResource");
            
    //private static ResourceBundle resource1 = (ResourceBundle) request.getSession().getAttribute("resourceBundle");
        
    public boolean supports(Class clazz) {
        return LMSForm.class.equals(clazz);
    }
    public void validate(Object obj, Errors errors) {
        validateAddSubject((LMSForm)obj,errors) ;
        validateAddLesson((LMSForm)obj,errors) ;
        validateAddItem((LMSForm)obj,errors) ;
        validateEditItem((LMSForm)obj,errors) ;
        validateAddDoc((LMSForm)obj,errors) ;
        validateEditDoc((LMSForm)obj,errors) ;
        validateAddEntrance((LMSForm)obj,errors) ;
        validateEditEntrance((LMSForm)obj,errors) ;
    }  
    public void validateAddSubject(LMSForm lMSForm, Errors errors) {
        String name = lMSForm.getSubjectName();
        String code = lMSForm.getSubjectCode();  
        String[] subjectName = {resource.getMessage(new DefaultMessageSourceResolvable("subjectName"), locale)}; 
        String[] subjectCode = {resource.getMessage(new DefaultMessageSourceResolvable("subjectCode"), locale)};     
        if (name==null || name.equals("")) {
              ValidationUtils.rejectIfEmptyOrWhitespace(errors, "subjectName", "subjectName", subjectName, "subjectName must not be empty");         
        }
        if (code==null || code.equals("")) {
            ValidationUtils.rejectIfEmptyOrWhitespace(errors, "subjectCode", "subjectCode", subjectCode, "subjectCode must not be empty");          
        } 
    }

    public void validateUpdateFileDoc(LMSForm lMSForm, Errors errors) {
//        lMSForm.setErrorMode("");
//        if (lMSForm.isZipfile()) {
//            String[] itemName = {resource.getMessage(new DefaultMessageSourceResolvable("itemName"), locale)};
//              ValidationUtils.rejectIfEmptyOrWhitespace(errors, "docFilename", "itemName", itemName, "docFilename must not be empty");
//        }
//       clearZipfile(lMSForm, errors);
    }

     public void validateUpdateFileItem(LMSForm lMSForm, Errors errors) {
//        lMSForm.setErrorMode("");
//        if (lMSForm.isZipfile()) {
//            String[] itemName = {resource.getMessage(new DefaultMessageSourceResolvable("itemName"), locale)};
//              ValidationUtils.rejectIfEmptyOrWhitespace(errors, "docFilename", "itemName", itemName, "docFilename must not be empty");
//        };
//
//       clearZipfile(lMSForm, errors);
    }
//     public void clearZipfile(LMSForm lMSForm, Errors errors){
//         if (errors.getErrorCount()>0){
//                    lMSForm.setZipfile(false);
//        };
//     }

    public void validateUpdateFileEn(LMSForm lMSForm, Errors errors) {
//        lMSForm.setErrorMode("");
//        if (lMSForm.isZipfile()) {
//            String[] itemName = {resource.getMessage(new DefaultMessageSourceResolvable("itemName"), locale)};
//              ValidationUtils.rejectIfEmptyOrWhitespace(errors, "docFilename", "itemName", itemName, "docFilename must not be empty");
//
//        }
//         clearZipfile(lMSForm, errors);
    }
  
        public void validateAddLesson(LMSForm lMSForm, Errors errors) {
        String lCode = lMSForm.getLessonCode();
        String lName = lMSForm.getLessonName(); 
        String[] lessonCode = {resource.getMessage(new DefaultMessageSourceResolvable("lessonCode"), locale)}; 
        String[] lessonName = {resource.getMessage(new DefaultMessageSourceResolvable("lessonName"), locale)};
        if (lCode==null || lCode.equals("")) {
              ValidationUtils.rejectIfEmptyOrWhitespace(errors, "lessonCode", "lessonCode", lessonCode, "lessonCode must not be empty");             
        }
        if (lName==null || lName.equals("")) {
            ValidationUtils.rejectIfEmptyOrWhitespace(errors, "lessonName", "lessonName", lessonName, "lessonName must not be empty");    
        }       
    }


        public void validateBaseItem(LMSForm lMSForm, Errors errors) {
            String[] itemName = {resource.getMessage(new DefaultMessageSourceResolvable("itemName"), locale)};
            String[] itemKeyword = {resource.getMessage(new DefaultMessageSourceResolvable("itemKeyword"), locale)};
            String[] selectType = {resource.getMessage(new DefaultMessageSourceResolvable("selectType"), locale)};
            String[] descriptionArg = {resource.getMessage(new DefaultMessageSourceResolvable("LMSForm.description"), locale)};
            String[] itemDescription = {resource.getMessage(new DefaultMessageSourceResolvable("itemDescription"), locale)};

             ValidationUtils.rejectIfEmptyOrWhitespace(errors, "itemKeyword", "itemKeyword", itemKeyword, "itemKeyword must not be empty");
             ValidationUtils.rejectIfEmptyOrWhitespace(errors, "selectType", "selectType", selectType, "selectType must not be empty");
//             ValidationUtils.rejectIfEmptyOrWhitespace(errors, "itemName", "LMSForm.itemName", itemName, "item-name must not be empty");
//             ValidationUtils.rejectIfEmptyOrWhitespace(errors, "itemDescription", "errors.required", descriptionArg, "description must not be empty");
             ValidationUtils.rejectIfEmptyOrWhitespace(errors, "itemName", "itemName", itemName, "item-name must not be empty");
             ValidationUtils.rejectIfEmptyOrWhitespace(errors, "itemDescription", "itemDescription", itemDescription, "description must not be empty");
        }

         public void validateStatusItem(LMSForm lMSForm, Errors errors) {
             String[] commentArg = {resource.getMessage(new DefaultMessageSourceResolvable("LMSForm.comment"), locale)};
            ValidationUtils.rejectIfEmptyOrWhitespace(errors, "commentItem", "errors.required", commentArg, "comment must not be empty");
         }

        public void validateAddItem(LMSForm lMSForm, Errors errors) {
        lMSForm.setErrorMode("");
        String type = lMSForm.getSelectType();
        String Keyword = lMSForm.getItemKeyword();
        String name = lMSForm.getItemName();

        String lesson = lMSForm.getSelectLesson();


        
        ValidationUtils.rejectIfEmptyOrWhitespace(errors, "selectSubject", "error.subjectNotFound",  "select Subject");
       
        ValidationUtils.rejectIfEmptyOrWhitespace(errors,"selectLesson", "lessonError",  "select correct Lesson");
       
        String[] itemName = {resource.getMessage(new DefaultMessageSourceResolvable("itemName"), locale)}; 
        String[] itemKeyword = {resource.getMessage(new DefaultMessageSourceResolvable("itemKeyword"), locale)};
        String[] selectType = {resource.getMessage(new DefaultMessageSourceResolvable("selectType"), locale)};
   
//        if (lMSForm.isZipfile()) {
//              ValidationUtils.rejectIfEmptyOrWhitespace(errors, "docFilename", "itemName", itemName, "docFilename must not be empty");
//        }
        if (Keyword==null || Keyword.equals("")) {
            ValidationUtils.rejectIfEmptyOrWhitespace(errors, "itemKeyword", "itemKeyword", itemKeyword, "itemKeyword must not be empty");       
        }
         if (type==null || type.equals("0")) {
            ValidationUtils.rejectIfEmptyOrWhitespace(errors, "selectType", "selectType", selectType, "selectType must not be empty");       
        }
//         lMSForm.setZipfile(false);
    }
      public void validateEditItem(LMSForm lMSForm, Errors errors) {       
        String Keyword = lMSForm.getItemKeyword();
        String name = lMSForm.getItemName();
        String[] itemName = {resource.getMessage(new DefaultMessageSourceResolvable("itemName"), locale)};
        String[] itemKeyword = {resource.getMessage(new DefaultMessageSourceResolvable("itemKeyword"), locale)};
       
        if (name==null || name.equals("")) {
              ValidationUtils.rejectIfEmptyOrWhitespace(errors, "itemName", "itemName", itemName, "itemName must not be empty");
        }
        if (Keyword==null || Keyword.equals("")) {
            ValidationUtils.rejectIfEmptyOrWhitespace(errors, "itemKeyword", "itemKeyword", itemKeyword, "itemKeyword must not be empty");       
        }
     
    }
          
       public void validateAddDoc(LMSForm lMSForm, Errors errors) {
        String name = lMSForm.getDocName();
        String owner = lMSForm.getDocOwner();
        String type = lMSForm.getDocType(); 
//        String dfile = "";
//       
//                if(lMSForm.getDocFile()!=null){
//                    dfile =lMSForm.getDocFile().getFileName();
//                }
        
        String[] docName = {resource.getMessage(new DefaultMessageSourceResolvable("docName"), locale)}; 
        String[] docOwner = {resource.getMessage(new DefaultMessageSourceResolvable("docOwner"), locale)};
        String[] docType = {resource.getMessage(new DefaultMessageSourceResolvable("docType"), locale)};
//        String[] docFile = {resource.getMessage(new DefaultMessageSourceResolvable("docFile"), locale)};
   
        if (name==null || name.trim().equals("")) {
              ValidationUtils.rejectIfEmptyOrWhitespace(errors, "docName", "docName", docName, "docName must not be empty");           
        }
//         lMSForm.setZipfile(false);
        if (owner==null || owner.trim().equals("")) {
            ValidationUtils.rejectIfEmptyOrWhitespace(errors, "docOwner", "docOwner", docOwner, "docOwner must not be empty");       
        }
         if (type==null || type.trim().equals("0")) {
            ValidationUtils.rejectIfEmptyOrWhitespace(errors, "docType", "docType", docType, "docType must not be empty");       
        }
//        if (dfile==null || dfile.equals("")) {
//            ValidationUtils.rejectIfEmptyOrWhitespace(errors, "docFile", "docFile", docFile, "docFile must not be empty");       
//        }
    }
        public void validateEditDoc(LMSForm lMSForm, Errors errors) {
        String name = lMSForm.getDocName();
        String owner = lMSForm.getDocOwner();
        String type = lMSForm.getDocType(); 
        
        
        String[] docName = {resource.getMessage(new DefaultMessageSourceResolvable("docName"), locale)}; 
        String[] docOwner = {resource.getMessage(new DefaultMessageSourceResolvable("docOwner"), locale)};
        String[] docType = {resource.getMessage(new DefaultMessageSourceResolvable("docType"), locale)};
        
        if (name==null || name.equals("")) {
              ValidationUtils.rejectIfEmptyOrWhitespace(errors, "docName", "docName", docName, "docName must not be empty");           
        }
        if (owner==null || owner.equals("")) {
            ValidationUtils.rejectIfEmptyOrWhitespace(errors, "docOwner", "docOwner", docOwner, "docOwner must not be empty");       
        }
         if (type==null || type.equals("0")) {
            ValidationUtils.rejectIfEmptyOrWhitespace(errors, "docType", "docType", docType, "docType must not be empty");       
        }
       
    }
        public void validateAddEntrance(LMSForm lMSForm, Errors errors) {
        String year = lMSForm.getSelectYear();
        String number = lMSForm.getSelectNumber();
        String name = lMSForm.getEnName(); 
        String owner = lMSForm.getEnOwner(); 
//        String efile = lMSForm.getEnFile().getFileName(); 
//       
//        String[] enFile = {resource.getMessage(new DefaultMessageSourceResolvable("enFile"), locale)};
        String[] enName = {resource.getMessage(new DefaultMessageSourceResolvable("enName"), locale)}; 
        String[] enOwner = {resource.getMessage(new DefaultMessageSourceResolvable("enOwner"), locale)};
        String[] selectNumber = {resource.getMessage(new DefaultMessageSourceResolvable("selectNumber"), locale)};
        String[] selectYear = {resource.getMessage(new DefaultMessageSourceResolvable("selectYear"), locale)};
        
        
        if (name==null || name.equals("")) {
              ValidationUtils.rejectIfEmptyOrWhitespace(errors, "enName", "enName", enName, "enName must not be empty");           
        }
        if (owner==null || owner.equals("")) {
            ValidationUtils.rejectIfEmptyOrWhitespace(errors, "enOwner", "enOwner", enOwner, "enOwner must not be empty");       
        }
         if (number==null ||  number.equals("0")) {
            ValidationUtils.rejectIfEmptyOrWhitespace(errors, "selectNumber", "selectNumber", selectNumber, "selectNumber must not be empty");       
        }
           if (year==null || year.equals("0")) {
            ValidationUtils.rejectIfEmptyOrWhitespace(errors, "selectYear", "selectYear", selectYear, "selectYear must not be empty");       
        }
//        if (efile==null || efile.equals("")) {
//            ValidationUtils.rejectIfEmptyOrWhitespace(errors, "enFile", "enFile", enFile, "enFile must not be empty");       
//        }
    }
        
        public void validateEditEntrance(LMSForm lMSForm, Errors errors) {
        String year = lMSForm.getSelectYear();
        String number = lMSForm.getSelectNumber();
        String name = lMSForm.getEnName(); 
        String owner = lMSForm.getEnOwner(); 

        String[] enName = {resource.getMessage(new DefaultMessageSourceResolvable("enName"), locale)}; 
        String[] enOwner = {resource.getMessage(new DefaultMessageSourceResolvable("enOwner"), locale)};
        String[] selectNumber = {resource.getMessage(new DefaultMessageSourceResolvable("selectNumber"), locale)};
        String[] selectYear = {resource.getMessage(new DefaultMessageSourceResolvable("selectYear"), locale)};
        
        
        if (name==null || name.equals("")) {
              ValidationUtils.rejectIfEmptyOrWhitespace(errors, "enName", "enName", enName, "enName must not be empty");           
        }
        if (owner==null || owner.equals("")) {
            ValidationUtils.rejectIfEmptyOrWhitespace(errors, "enOwner", "enOwner", enOwner, "enOwner must not be empty");       
        }
         if (number==null || number.equals("0")) {
            ValidationUtils.rejectIfEmptyOrWhitespace(errors, "selectNumber", "selectNumber", selectNumber, "selectNumber must not be empty");       
        }
           if (year==null || year.equals("0")) {
            ValidationUtils.rejectIfEmptyOrWhitespace(errors, "selectYear", "selectYear", selectYear, "selectYear must not be empty");       
        }
    }
        
  
    public ResourceBundleMessageSource getResource() {
        return resource;
    }

    public void setResource(ResourceBundleMessageSource resource) {
        this.resource = resource;
    }
        
}


